package com.wwj.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.wwj.gulimall.product.entity.BrandEntity;
import com.wwj.gulimall.product.entity.CategoryEntity;
import com.wwj.gulimall.product.service.BrandService;
import com.wwj.gulimall.product.service.CategoryService;
import com.wwj.gulimall.product.vo.BrandVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.wwj.gulimall.product.entity.CategoryBrandRelationEntity;
import com.wwj.gulimall.product.service.CategoryBrandRelationService;
import com.wwj.common.utils.PageUtils;
import com.wwj.common.utils.R;

import javax.annotation.Resource;


/**
 * 品牌分类关联
 *
 * @author wwj
 * @email 3288937519@qq.com
 * @date 2024-09-10 16:25:39
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Resource
    private CategoryBrandRelationService categoryBrandRelationService;
    @Resource
    BrandService brandService;
    @Resource
    CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:categorybrandrelation:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 根据品牌id找到关联分类
     */
    @GetMapping("/catelog/list")
    public R relationCatelog(@RequestParam Map<String, Object> params){
        List<CategoryBrandRelationEntity> entityList
                = categoryBrandRelationService.queryByBrandId(params);
        return R.ok().put("data",entityList);
    }

    /**
     * 获取分类关联的品牌
     */
    @GetMapping("/brands/list")
    public R relationBrand(@RequestParam(value="catId") Long catId){
        List<CategoryBrandRelationEntity> entityList
                = categoryBrandRelationService.queryByCatId(catId);

        List<BrandVo> brandVos = entityList.stream().map((entity)->{
            BrandVo brandVo = new BrandVo();
            BeanUtils.copyProperties(entity, brandVo);
            return brandVo;
        }).collect(Collectors.toList());
        return R.ok().put("data", brandVos);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("product:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
        System.out.println(categoryBrandRelation);
        // 得到品牌名称
        BrandEntity brand = brandService.getById(categoryBrandRelation.getBrandId());
        categoryBrandRelation.setBrandName(brand.getName());
        // 得到分类名
        CategoryEntity category = categoryService.getById(categoryBrandRelation.getCatelogId());
        categoryBrandRelation.setCatelogName(category.getName());
        // 保存到 品牌-分类 关联表中
        categoryBrandRelationService.save(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
